﻿package com.dLibs.photosAntiga
{	
	import com.dLibs.events.MouseEvents;
	import com.dLibs.utils.scaleObject;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.net.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	
	public class gallLoader extends MovieClip
	{
		/* @ Variables	 				____________________________________________________*/
		
		private var _minData		:Array;
		private var _maxData		:Array;
		private var _thmBox			:Array;
		private var _bigPict		:MovieClip;
		private var _smallPict		:MovieClip;
		private var _thmClip		:MovieClip;
		private var _thmMovie		:MovieClip;
		private var _bigMovie		:MovieClip;
		private var _loaderClip		:MovieClip;
		private var _fullClip		:MovieClip;
		private var _fullSize		:String;
		private var _thumbClass		:String;
		private var _loaderThumb	:Loader;
		private var _loaderBig		:Loader;
		private var _i				:uint;
		private var _thumbPadding	:uint;
		private var _thumbLines		:uint;
		private var _thumbLineY		:uint;
		private var _thumbLineX		:uint;
		private var _wScale			:Number;
		private var _hScale			:Number;
		private var _moreScale		:Number;
		private var _imageZoom		:Boolean;
		private var _imgWidth		:Number;
		private var _imgHeight		:Number;
		private var newFull			:showFull;
		private var fullLoader		:Loader;
		
		private var _moreZoom		:MovieClip;
		private var _lessZoom		:MovieClip;
		private var maxWidth		:Number;
		private var maxHeight		:Number;
		private var minWidth		:Number;
		private var minHeight		:Number;
		private var growImage		:Number = 0.2;
		private var iGrow			:uint;
		private var _dragObject		:MovieClip;
		private var clips			:Array;//Var Joisiney
		private var listPhotos		:Array; //var joisiney
		
		/* @ Constructor 				____________________________________________________*/
		
		public function gallLoader(minData:Array,maxData:Array,bigPicture:MovieClip,smallPictures:MovieClip,thumbClass:String,thumbPadding:uint=0, thumbLines:uint=1, moreScale:Number=1, imageZoom:Boolean = false, zoomButtons:Object = null, loaderClip:MovieClip=null,fullSize:String=null):void
		{
			_minData 		= minData;
			_maxData 		= maxData;
			_bigPict 		= bigPicture;
			_smallPict 		= smallPictures;
			_thumbClass 	= thumbClass;
			_thumbPadding 	= thumbPadding;
			_thumbLines		= thumbLines;
			_loaderClip		= loaderClip;
			_moreScale		= moreScale;
			_imageZoom		= imageZoom;
			_fullSize		= fullSize;
			
			if ( _imageZoom ) 
			{
				_moreZoom = zoomButtons.moreZoom;
				_lessZoom = zoomButtons.lessZoom;
			}
			
			_thmBox = new Array();
			initGall();
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, removeData);
		};
		
		/* @ init Function 				____________________________________________________*/
		
		private function initGall():void
		{
			if ( _loaderClip != null ) _loaderClip.visible = false;
			
			clips 		= new Array();
			listPhotos 	= new Array();
			
			for ( _i = 0; _i < _minData.length; _i++ )
			{
				_loaderThumb 	= new Loader();
				
				listPhotos.push(_loaderThumb);
				
				_loaderThumb.x 	= _thumbPadding;
				_loaderThumb.y 	= _thumbPadding;
				_loaderThumb.load(new URLRequest(_minData[_i]));
				_loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE, showThumb);
								
				_thmClip 	= new ( getDefinitionByName(_thumbClass) as Class )();
				_thmClip.addChild(_loaderThumb);
				
				_wScale = (_thmClip.width - _thumbPadding*2) * _moreScale; 
				_hScale = (_thmClip.height - _thumbPadding*2) * _moreScale;
				
				_thmMovie 		= new MovieClip();
				clips.push(_thmMovie);
				_thmMovie.x 	= ( _thmClip.width + 5 ) * _thumbLineX;
				_thmMovie.y		= ( _thmClip.height + 5 ) * _thumbLineY;				
				_thmMovie.id 	= _i;
				_thmMovie.buttonMode = true;
				
				_thmMovie.addEventListener(MouseEvent.CLICK, viewPicture);
				_thmBox.push(_thmMovie);
				_thmMovie.addChild(_thmClip);
				
				_smallPict.addChild(_thmMovie);
				
				if ( _thumbLineY + 1 < _thumbLines ) 
				{
					_thumbLineY++;
				} 
				else 
				{
					_thumbLineY = 0;
					_thumbLineX++;
				}
			};
			_thmBox[0].dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		};
		
		private function removeData(e:Event):void
		{
			var imageQnt:uint = listPhotos.length;
			
			for ( var iq:uint = 0; iq < imageQnt; iq++ )
			{
				( listPhotos[iq] as Loader ).unload();
			}
		}
		
		public function get jrs_ArrClips():Array
		{
			return clips;
		}
		public function get jrs_ArrListPhotos():Array
		{
			return listPhotos;
		}
		private function allBlackWhite():void
		{
			var ma:uint = _thmBox.length;
			for ( var iii:uint = 0; iii < ma; iii++ )
			{
				TweenMax.to(_thmBox[iii], .3, {colorMatrixFilter:{ saturation : 0 }});
			}
		}
		
		private function showThumb(e:Event):void
		{
			var so:scaleObject = new scaleObject(e.currentTarget.content,{width:_wScale,height:_hScale});
		}
		
		/* @ init Function 				____________________________________________________*/
		
		private function viewPicture(e:MouseEvent):void
		{
			_loaderBig = new Loader();
			
			listPhotos.push(_loaderBig);
			
			if ( _fullSize != null )
			{
				try
				{
					fullLoader.unload();
				}
				catch(e:Error){}
				
				fullLoader = new Loader();
				fullLoader.load(new URLRequest(_maxData[e.currentTarget.id]));
			}
			
			_loaderBig.load(new URLRequest(_maxData[e.currentTarget.id]));
			_loaderBig.contentLoaderInfo.addEventListener(Event.COMPLETE, showLoadedBig);
			_loaderBig.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showLoaderBig);
			allBlackWhite();
			TweenMax.to(e.currentTarget, .7, {colorMatrixFilter:{ saturation : 1 }});			
			
			if ( _bigPict.numChildren > 1 ) _bigPict.removeChildAt(1);
			
			dispatchEvent(new MouseEvents(MouseEvents.CLICK, { id : e.currentTarget.id }));
		};
		
		private function showLoaderBig(e:ProgressEvent):void
		{
			try
			{
				_loaderClip.visible = true;
			}
			catch(error:Error)
			{
			}
		};
		
		private function showLoadedBig(e:Event):void
		{
			try
			{
				_loaderClip.visible = false;
			}
			catch(error:Error)
			{
			}
			
			var bmp:Bitmap = e.currentTarget.content;
				bmp.smoothing = true;
			
			_bigMovie 		= new MovieClip();
			_bigMovie.alpha = 0;
			_bigMovie.addChild(_loaderBig);
			_bigPict.addChild(_bigMovie);
			
			if ( _fullSize != null )
			{
				_bigPict.buttonMode = true;
				_bigPict.mouseChildren = false;
				_bigPict.addEventListener(MouseEvent.CLICK, showFullSize);
			}
			
			_bigPict.scaleX = 1;
			_bigPict.scaleY = 1;
			
			if ( _imageZoom )
			{
				_bigPict.x = _dragObject.x;
				_bigPict.y = _dragObject.y;
				
				maxWidth 				= _bigPict.width;
				maxHeight				= _bigPict.height;
				
				_imgWidth				= _dragObject.width;
				_imgHeight				= _dragObject.height;
				
				var iLoaded:scaleObject = new scaleObject(e.currentTarget.content,{ width:_imgWidth, height:_imgHeight });
				
				_moreZoom.buttonMode 	= true;
				_lessZoom.buttonMode 	= true;   
				
				minWidth 				= _bigPict.width;
				minHeight				= _bigPict.height;
				
				_moreZoom.addEventListener(MouseEvent.CLICK, ImageplusZoom);
				_lessZoom.addEventListener(MouseEvent.CLICK, ImagelessZoom);
				
				_bigPict.addEventListener(MouseEvent.MOUSE_DOWN, initDrag);
				_bigPict.stage.addEventListener(MouseEvent.MOUSE_UP, stopDraging);
			}
			
			TweenLite.to(_bigMovie,.4,{ alpha : 1 });
		};
		
		/* SHOW FULLSIZE PICTURE ---------------------------------------------------------------------*/
		
		private function showFullSize(e:MouseEvent):void
		{
			newFull = new showFull();
			newFull.fechar.addEventListener(MouseEvent.CLICK, fecharJanela);
			newFull.x = ( 855 - 450 ) / 2;
			newFull.y = ( 465 - 245 ) / 2;
			newFull.alpha = 0;
			newFull.addChild(fullLoader);
				
			TweenLite.to(newFull,.5, { alpha : 1 });
				
			_bigPict.stage.addChild(newFull);
		}
		
		private function fecharJanela(e:MouseEvent):void
		{
			TweenLite.to(newFull,.5, { alpha : 0, onComplete : function():void
				{
					_bigPict.stage.removeChild(newFull);
				}			
			});
		}
		
		/* END SHOW FULLSIZE PICTURE ------------------------------------------------------------------*/
		
		public function set setDragObject(dragObject:MovieClip):void
		{
			if ( _imageZoom )
			{
				_dragObject = dragObject;
			}
		}
		
		private function initDrag(event:Event):void 
		{
			var _x:Number = _bigPict.width 	- 	_dragObject.width;
			var _y:Number = _bigPict.height - 	_dragObject.height;
			
			if ( _x < 0 ) _x = 0;
			if ( _y < 0 ) _y = 0;
			
			var rec:Rectangle = new Rectangle(_dragObject.x - _x, _dragObject.y - _y ,_x, _y);
			_bigPict.startDrag(false,rec);
		}
		
		private function ImageplusZoom(m:MouseEvent):void
		{
			_bigPict.scaleX = _bigPict.scaleY += 0.2;
		}
		
		private function ImagelessZoom(m:MouseEvent):void
		{
			_bigPict.scaleX = _bigPict.scaleY -= 0.2;
			verifyZoom();
		}
		
		private function verifyZoom():void
		{
			if ( _bigPict.width < _dragObject.width || _bigPict.height < _dragObject.height )
			{
				_bigPict.scaleX = _bigPict.scaleY += 0.2;
			}
			if ( (_bigPict.x + _bigPict.width) < ( _dragObject.x + _dragObject.width ) ) _bigPict.x = (_dragObject.x + _dragObject.width) - _bigPict.width; 
			if ( (_bigPict.y + _bigPict.height) < ( _dragObject.y + _dragObject.height ) ) _bigPict.y = (_dragObject.y + _dragObject.height) - _bigPict.height;
		}		
		
		private function stopDraging(event:MouseEvent):void 
		{
			_bigPict.stopDrag();
		}
		
		/* @ End		 				____________________________________________________*/
		
	};
};